import Mock from 'mockjs'

// 自定义随机数据
Mock.Random.extend({
  dorm: function() {
    const dormList = ['一善书院', '双馨书院', '三创书院', '四实书院', '八方书院', '拾德书院']
    return this.pick(dormList)
  }
})

// 登录接口
Mock.mock('/api/login', 'post', (options) => {
  const postData = JSON.parse(options.body)
  const userData = localStorage.getItem('user')

  if (!userData) {
    return { status: 400, message: '用户不存在，请先注册!' }
  }

  const compareData = JSON.parse(userData)

  if (postData.username !== compareData.username) {
    return { status: 400, message: '用户名错误!' }
  }

  if (postData.password !== compareData.password) {
    return { status: 400, message: '密码错误!' }
  }

  return { status: 200, message: 'OK', username: postData.username }
})

// 学生信息接口
Mock.mock('/api/student-infos', 'get', {
  'data|10': [
    {
      id: '@id',
      name: '@cname',
      dorm: '@dorm',
      'score|1-100': 1
    }
  ]
})
